package com.mapapi;

import java.util.List;

import android.graphics.drawable.Drawable;
import android.location.Criteria;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;

import com.google.android.maps.*;

public class GoogleMapActivity extends MapActivity {

	MapView map = null;
	LocationManager locaMana;
	MyLocationOverlay mylocation;
	GeoPoint point, mypoint;
	Location location;
	int intDistance;
	float distance;
	
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_google_map);
        map = (MapView)findViewById(R.id.map);
        map.getController().setZoom(16);
  
        GeoPoint bachkhoa = new GeoPoint((int)(21.006357*1000000.0),(int)(105.8429911*1000000.0));
       
       
        map.setBuiltInZoomControls(true);
        locaMana = (LocationManager)getSystemService(LOCATION_SERVICE);
        //lay toa do vi tri hien tai
        location = locaMana.getLastKnownLocation(locaMana.getBestProvider(new Criteria(), true));
        GeoPoint loca = new GeoPoint((int)(location.getLatitude()*1000000.0), (int)(location.getLongitude()*1000000.0));
        
        map.getController().animateTo(loca);
        
        //hien thi vi tri hien tai tren ban do
        mylocation = new MyLocationOverlay(this, map);
        mylocation.enableMyLocation();
        mylocation.enableCompass();
       //point = mylocation.getMyLocation();
        map.getOverlays().add(mylocation);    
    	map.postInvalidate(); 
    	
      
      
    	//hien thi mot vi tri nao tren ban do
        List mapOverlays = map.getOverlays();
        Drawable drawable = this.getResources().getDrawable(R.drawable.icon_map);
        
        MyItemizedOverlay itemizedoverlay = new MyItemizedOverlay(drawable,this);
        OverlayItem overlayitem = new OverlayItem(bachkhoa, "Xin Chào!", "Trường Đại Học Bách Khoa Hà Nội");
       //OverlayItem overlay2 = new OverlayItem(loca, "Xin Chào", "Bạn đang ở tọa độ (" +location.getLatitude() + "," + location.getLongitude()+")");
        itemizedoverlay.addOverlay(overlayitem);                
        //itemizedoverlay.addOverlay(overlay2);	     	
        mapOverlays.add(itemizedoverlay);    
        

        //cảnh báo khi đang ở gần trường bách khoa
        Location q = new Location("bach khoa");
        q.setLatitude(21.006357);
        q.setLongitude(105.8429911);
        distance = location.distanceTo(q);
        intDistance = Math.round(distance);
       
        View reportView = getLayoutInflater().inflate(R.layout.report, null);
       
       if(intDistance < 200){
	             
	        AlertDialog.Builder report = new AlertDialog.Builder(this);
	        report.setTitle("Thông Báo");
	        report.setView(reportView);
	        report.setNegativeButton("OK", null);
	        report.show();
       }
        
   }
    
        
    @Override
	protected boolean isRouteDisplayed() {
		// TODO Auto-generated method stub
		return false;
	}

    
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.activity_google_map, menu);
        return true;
    }
    
    

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		// TODO Auto-generated method stub
		if(item.getItemId()==R.id.menu_location){
			//locaMana.requestLocationUpdates(LocationManager.GPS_PROVIDER, 1000, 1, onLocationChange);
			
			String loca = "Bạn đang ở tọa độ ( " + location.getLatitude() + "," +
					location.getLongitude()+ " )";
			Toast.makeText(getApplicationContext(),loca , Toast.LENGTH_LONG).show();
		    
	        
			return true;
		}
		else{
			Toast.makeText(getApplicationContext(), "no setting", Toast.LENGTH_LONG).show();

	        
		}
	       
		return super.onOptionsItemSelected(item);
		
	}
	

	LocationListener onLocationChange = new LocationListener( ) {
		
		public void onStatusChanged(String provider, int status, Bundle extras) {
			// TODO Auto-generated method stub
			
		}
		
		public void onProviderEnabled(String provider) {
			// TODO Auto-generated method stub
			Toast.makeText(getApplication(), "GPS off", 1).show();
			
		}
		
		public void onProviderDisabled(String provider) {
			// TODO Auto-generated method stub
			
		}
		
		public void onLocationChanged(Location location) {
			// TODO Auto-generated method stub
			String loca = "You are at ( " + location.getLatitude() + ";" +
					location.getLongitude()+ " )";
			Toast.makeText(getApplicationContext(),loca , Toast.LENGTH_LONG).show();
			locaMana.removeUpdates(onLocationChange);
			
		}
	};
	
	

	@Override
	protected void onPause() {
		// TODO Auto-generated method stub
		mylocation.disableMyLocation();
		super.onPause();
	}

	@Override
	protected void onResume() {
		// TODO Auto-generated method stub
		super.onResume();
		mylocation.enableMyLocation();	
	}
	
	
}
